using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class BirdControl : MonoBehaviour
{
    public int Force=50;
    private GameObject barriers;
    private Rigidbody2D rig;
    // Start is called before the first frame update
    void Start()
    {
        barriers = GameObject.Find("Barriers");
        rig=GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    void OnCollisionEnter2D(Collision2D collision)
    {
        if(collision.gameObject.CompareTag("Barrier"))
        {
            if(GMGlobal.Instance.birdLive>0)
            {
                GMGlobal.Instance.birdLive-=1;
                GMGlobal.Instance.UpdateLive();
                // 清除屏幕中所有障碍物
                GMGlobal.Instance.ClearChilds(barriers.transform);
                // 
                this.transform.localPosition = new Vector2(0, 0);

            }
            else
            {
                GMGlobal.Instance.birdStatus=0;
                SceneManager.LoadScene("GameOver");
            }
        }
    }

    public void Fly()
    {
        if(GMGlobal.Instance.birdLive>0)
        {
            // Debug.Log("Fly");
            rig.AddForce(Force * Vector2.up);
        }
    }
}
